﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Styx.CommonBot;
using Styx.Common;
using System.Windows.Media;
using Styx.WoWInternals.WoWObjects;
using Styx;
using Styx.CommonBot.Coroutines;

using units = Paster.Managers.Units;
using movement = Paster.Managers.Movement;
using groups = Paster.Managers.Groups;
using spells = Paster.Spell.Spells;
using _ds = Paster.Shadow.ShadowSettings;
using Styx.WoWInternals;

using P = Paster.GUI.CommonSettings;
using Movement = Paster.Managers.Movement;

namespace Paster.Shadow
{
    partial class Rotations
    {
        public static bool MeSolo { get { return groups.partyCount == 0; } }
        public static bool gotTarget { get { return Me.CurrentTarget != null && units.ValidUnit(Me.CurrentTarget); } }
        private static LocalPlayer Me { get { return StyxWoW.Me; } }

        public static async Task<bool> ShadowRoutine()
        {

            

            await CommonCoroutines.SleepForLagDuration();
            return true;
        }


        public static int countMindSpike = 0;
        public static bool needDotRot = false;
        public static WoWGuid lstGuid;

        public static async Task<bool> ClarityOfPowerRoutine()
        {
            if (Me.Mounted && !units.AutoBot && Me.IsMoving) return false;
            if (Me.IsCasting || Me.IsChanneling) return false;
            if (Main.pullTimer.IsRunning) { Main.pullTimer.Stop(); }
            if (lstGuid != Me.CurrentTargetGuid) { lstGuid = Me.CurrentTargetGuid; needDotRot = false; }
            if (await spells.CastBuff(shadowform, !Me.HasAura(shadowform), Me)) return true;

            if (P.myPrefs.AutoMovement) await Movement.MoveToCombatTarget();
            if (P.myPrefs.AutoFacing) await Movement.FaceMyCurrentTarget();
            if (await Movement.ClearMyDeadTarget()) return true;

            if (await spells.Cast(spells.DesperatePrayer, Me.HealthPercent <= _ds.myPrefs.DesperatePrayerPrecent, Me)) return true;
            if (await spells.CastBuff(giftofthenaaru, Me.HealthPercent <= _ds.myPrefs.NaaruPercent, Me)) return true;
            if (await spells.UseItem(healthStone, needHealthstone())) return true;
            if (await (spells.CastBuff(powerwordshield, Me.HealthPercent <= _ds.myPrefs.PowerWordShieldPrecent
                && !units.spellOnCooldown(powerwordshield) 
                && !units.debuffExists("Weakened Soul", Me), Me))) return true;



            //-20%
            if (await spells.Cast(shadowworddead, CanCast && Me.CurrentTarget.HealthPercent < 20 && lastSpell != shadowworddead, Me.CurrentTarget)) return true;

            //+20%
            if (await Cast(mindblast, CanCast && !units.spellOnCooldown(mindblast), Me.CurrentTarget)) return true;
            if (await Cast(mindspike, CanCast && !units.spellOnCooldown(mindspike) && !units.MyDebuffExists(devouringplague, Me.CurrentTarget), Me.CurrentTarget)) return true;

            if (await Cast(shadowwordpain, CanCast
                && units.MyDebuffExists(devouringplague, Me.CurrentTarget)
                && !units.MyDebuffExists(shadowwordpain, Me.CurrentTarget) 
                && lastSpell != shadowwordpain, Me.CurrentTarget)) return true;
            if (await Cast(vampirictouch, CanCast
                && units.MyDebuffExists(devouringplague, Me.CurrentTarget)
                && !units.MyDebuffExists(vampirictouch, Me.CurrentTarget) 
                && lastSpell != vampirictouch, Me.CurrentTarget)) return true;
            if (await Cast(devouringplague, CanCast && orbCount == 5, Me.CurrentTarget)) return true;
            if (await Cast(devouringplague, CanCast && orbCount == 3 && lastSpell == mindflay, Me.CurrentTarget)) return true;
            if (await Cast(mindflay, CanCast && units.MyDebuffExists(devouringplague, Me.CurrentTarget), Me.CurrentTarget)) return true;

            await CommonCoroutines.SleepForLagDuration();
            return true;
        }


        public static async Task<bool> Cast(string Spell, bool reqs, WoWUnit target)
        {
            if (!reqs) return false;
            if (!SpellManager.HasSpell(Spell)) return false;
            if (!SpellManager.CanCast(Spell, target)) return false;
            if (!SpellManager.Cast(Spell, target)) return false;

            Logging.Write(Colors.Yellow, "Casting: " + Spell + " on: " + target.SafeName);
            lastSpell = Spell;
            await CommonCoroutines.SleepForLagDuration();
            return true;
        }
    }
}
